package com.collectplus.express.order;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.menu.WebViewActivity;
import com.collectplus.express.model.CouponBean;
import com.collectplus.express.model.OrderBean;
import com.collectplus.express.model.OrderPriceBean;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.umeng.UMeng;
import droid.frame.utils.lang.Str;

/**
 * 收银台 - 订单价格明细列表()
 *
 * @author coffee <br>
 *         2016-1-20下午12:40:07
 */
public class OrderPriceAdapter extends BaseAdapterExt<OrderPriceBean> {

    private CouponBean coupon;

    // private String totalPrice;

    public OrderPriceAdapter(OrderBean order, Activity context) {
        super(null, context);
        items.clear();
        OrderPriceBean price1 = new OrderPriceBean();
        if (order.getIsCover() == 1) {
            price1.setName("补差价(超重" + order.getOrder2Weight() + "公斤)");
        } else {
            price1.setName("运费");
        }
        price1.setPrice(Str.formatPrice(order.getCost()));
        items.add(price1);
        // 如果上门服务费 > 0
        if (Str.parseFloat(order.getServiceCost()) > 0) {
            OrderPriceBean price2 = new OrderPriceBean();
            price2.setName("上门服务费");
            price2.setPrice(order.getServiceCost());
            items.add(price2);
        }
        // totalPrice = MathUtils.add(price1.getPrice(), price2.getPrice()) + "";
        setItems(items, false);
    }

    @Override
    public int getCount() {
        return super.getCount() + (coupon != null ? 1 : 0);
    }

    public void setCoupon(CouponBean coupon) {
        this.coupon = coupon;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.pay_price_item, parent, false);
            holder = new ViewHolder();
            holder.icon = convertView.findViewById(R.id.order_price_icon);
            holder.name = (TextView) convertView.findViewById(R.id.order_price_name);
            holder.price = (TextView) convertView.findViewById(R.id.order_price_money);
            holder.discountDesc = (TextView) convertView.findViewById(R.id.discountdesc_tv);
            holder.help = (ImageView) convertView.findViewById(R.id.help_iv);
            holder.period = (TextView) convertView.findViewById(R.id.order_period_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 最后一个是邮票
        if (coupon != null && position == getCount() - 1) {
            holder.name.setText("邮票");
            // if (MathUtils.sub(totalPrice, coupon.getCheapValue()) < 0) {
            // holder.price.setText("-" + MathUtils.sub(totalPrice, "0.01") + " 元");
            // } else {
            holder.price.setText("-" + coupon.getCheapValue() + "");
            // }
        } else {
            OrderPriceBean item = getItem(position);
            holder.name.setText(item.getName());
            holder.price.setText("￥" + Str.formatPrice(item.getPrice()) + "");
            holder.help.setVisibility(View.VISIBLE);
            holder.help.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    UMeng.click(context, "order_price_state_click");
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("title", "价格说明");
                    context.startActivityForResult(intent, 101);
                }
            });
        }
        if (position > 0) {
            holder.icon.setVisibility(View.INVISIBLE);
        } else {
            holder.icon.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    private class ViewHolder {
        private View icon;
        private TextView name;
        private TextView price;
        private TextView discountDesc;
        private ImageView help;
        private TextView period;
    }

}
